package com.esms.service.impl;

import com.esms.dao.ResponsibilityMapper;
import com.esms.po.Responsibility;
import com.esms.service.IResponsibilityService;
import com.esms.vo.ResponsibilityChangeVo;
import com.esms.vo.ResponsibilityDepVo;
import com.esms.vo.ResponsibilityJobVo;
import com.esms.vo.ResponsibilityShowVo;
import com.github.pagehelper.PageHelper;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.Date;
import java.util.List;

@Service
public class ResponsibilityServiceImpl implements IResponsibilityService {
    @Autowired
    ResponsibilityMapper responsibilityMapper;

    @Override
    public String getRespByUserId(Integer userId) {
        return responsibilityMapper.selectRespByUId(userId);
    }

    @Override
    public String getRespByJobAndDep(Integer job, Integer dep) {
        return responsibilityMapper.selectRespByJobAndDep(job, dep);
    }

    @Override
    public void AddDepResp(ResponsibilityDepVo responsibilityVo) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(ResponsibilityDepVo.class, Responsibility.class)
                .field("depId", "rDepartment")
                .field("content", "rContent")
                .byDefault().register();
        MapperFacade facade = mapperFactory.getMapperFacade();
        Responsibility responsibility = facade.map(responsibilityVo, Responsibility.class);
        responsibility.setrType("2");
        responsibility.setrJob(0);
        responsibilityMapper.insertResp(responsibility);

    }

    @Override
    public void AddJobResp(ResponsibilityJobVo responsibilityJobVo) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(ResponsibilityJobVo.class, Responsibility.class)
                .field("depId", "rDepartment")
                .field("jobId", "rJob")
                .field("content", "rContent")
                .byDefault().register();
        MapperFacade facade = mapperFactory.getMapperFacade();
        Responsibility responsibility = facade.map(responsibilityJobVo, Responsibility.class);
        responsibility.setrType("1");
        responsibilityMapper.insertResp(responsibility);
    }

    @Override
    public Integer countRep() {
        return responsibilityMapper.countByType(2);
    }

    @Override
    public Integer countJob() {
        return responsibilityMapper.countByType(1);
    }

    @Override
    public List<ResponsibilityShowVo> PageJobResp(Integer pageSize, Integer count, Integer depId, Integer jobId) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(ResponsibilityShowVo.class, Responsibility.class)
                .field("id", "rId")
                .field("depId", "rDepartment")
                .field("jobId", "rJob")
                .field("content", "rContent")
                .byDefault().register();
        MapperFacade facade = mapperFactory.getMapperFacade();
        PageHelper.startPage(count, pageSize);
        return facade.mapAsList(responsibilityMapper.ListResp(depId, jobId), ResponsibilityShowVo.class);
    }

    @Override
    public List<ResponsibilityShowVo> PageDepResp(Integer pageSize, Integer count, Integer depId) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(ResponsibilityShowVo.class, Responsibility.class)
                .field("id", "rId")
                .field("depId", "rDepartment")
                .field("jobId", "rJob")
                .field("content", "rContent")
                .byDefault().register();
        MapperFacade facade = mapperFactory.getMapperFacade();
        PageHelper.startPage(count, pageSize);
        return facade.mapAsList(responsibilityMapper.ListResp(depId, 0),ResponsibilityShowVo.class);
    }

    @Override
    public boolean checkIfExist(Integer depId, Integer jobId) {
        if (responsibilityMapper.exist(depId, jobId) != null) {
            return true;
        }
        return false;
    }

    @Override
    public void updateResp(ResponsibilityChangeVo responsibilityChangeVo) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(ResponsibilityChangeVo.class, Responsibility.class)
                .field("id", "rId")
                .field("depId", "rDepartment")
                .field("jobId", "rJob")
                .field("content", "rContent")
                .byDefault().register();
        MapperFacade facade = mapperFactory.getMapperFacade();
        Responsibility responsibility = facade.map(responsibilityChangeVo, Responsibility.class);
        responsibilityMapper.updateById(responsibility);
    }

    @Override
    public void DelById(Integer id) {
        responsibilityMapper.delById(id);
    }

    @Override
    public boolean checkIfExistExcluedId(Integer depId, Integer jobId, Integer id) {
        if (responsibilityMapper.existExcluedId(depId, jobId,id) != null) {
            return true;
        }
        return false;
    }
}
